<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
 * Publications List Model
 */
class NrmResearchModelPublications extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
					'id', 'a.id',
					'year_of_publication', 'a.year_of_publication',
					'title', 'a.title',
			);
		}
		parent::__construct($config);
	}
	protected function getListQuery()
	{
		// Create a new query object.
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		// Select some fields
		$query->select('a.*');
		// From the current project table
		$query->from('#__nrmresearch_publications as a');
	
		if ($this->getState('keywords')) {
			$query->where('a.keywords like "%'.$this->getState('keywords').'%"');
		}
	
		if ($this->getState('publication_title')) {
			$query->where('a.title like "%'.$this->getState('publication_title').'%"');
		}
	
		if ($this->getState('publication_filter')) {
			$where = 'a.title like "%'.$this->getState('publication_filter').'%" 
						or a.keywords like "%'.$this->getState('publication_filter').'%"
						or a.contact like "%'.$this->getState('publication_filter').'%"
						or a.phone like "%'.$this->getState('publication_filter').'%"
						or a.lead_organisation like "%'.$this->getState('publication_filter').'%"						
			';
			$query->where($where);
		}
		
		if ($this->getState('publication_investigators_author')) {
			$query->where('a.authors like "%'.$this->getState('publication_investigators_author').'%"');
		}
			
		if ($this->getState('lead_organisation')) {
			$query->where('a.lead_organisation like "%'.$this->getState('lead_organisation').'%"');
		}
	
		if ($this->getState('partner_organisation_s')) {
			$query->where('a.partner_organisation_s like "%'.$this->getState('partner_organisation_s').'%"');
		}
		$query->where('CONVERT( substring( `period_of_funding` , 6 ) , UNSIGNED INTEGER ) < YEAR( NOW( ))');
		//Add the list ordering clause.
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.id')).' '.$db->getEscaped($this->getState('list.direction', 'ASC')));
		//echo "start = ". $this->getState("list.start") . "    and limit = " . $this->getState("list.limit") ;
		//echo $query;//die;
		//var_dump(JRequest::get());
		return $query;
	}
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app	= JFactory::getApplication();
	
		// Load the parameters.		
		//$this->setState('params', $params);
	
		$db		= $this->getDbo();
	
		$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', MAX_NUMS_ITEMS_PER_PAGE);
		$this->setState('list.limit', 20);
		$perpage = JRequest::getVar('limit', 20, '', 'int');
		if($perpage != 0 )$this->setState('list.limit', $perpage);
	
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$this->setState('list.start', $limitstart);
	
		$orderCol	= JRequest::getVar('filter_order', 'id');
		if (!in_array($orderCol, $this->filter_fields)) {
			$orderCol = 'id';
		}
		$this->setState('list.ordering', $orderCol);
	
		$listOrder	=  JRequest::getCmd('filter_order_Dir', 'DESC');
		if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) {
			$listOrder = 'ASC';
		}
		$this->setState('list.direction', $listOrder);
	
		$keywords = JRequest::getVar('keywords','','','string');
		if($keywords==KEYWORD_LABEL)$keywords = '';
		$this->setState('keywords',$keywords);
	
		$publication_title = JRequest::getVar('publication_title','','','string');
		if($publication_title == TITLE_LABEL)$publication_title = '';
		$this->setState('publication_title',$publication_title);
		
		$publication_filter = JRequest::getVar('publication_filter','','','string');		
		$this->setState('publication_filter',$publication_filter);
	
		$publication_investigators_author = JRequest::getVar('publication_investigators_author','','','string');
		if($publication_investigators_author == AUTHOR_LABEL)$publication_investigators_author = '';
		$this->setState('publication_investigators_author',$publication_investigators_author);
	
		$lead_organisation = JRequest::getVar('lead_organisation','','','string');
		if($lead_organisation == LEAD_LABEL)$lead_organisation = '';
		$this->setState('lead_organisation',$lead_organisation);
	
		$partner_organisation_s = JRequest::getVar('partner_organisation_s','','','string');
		if($partner_organisation_s == PARTNER_LABEL)$partner_organisation_s = '';
		$this->setState('partner_organisation_s',$partner_organisation_s);
	
	}
}
